package org.fujay.langchain4jserver.domain.application.entity;

import lombok.*;
import org.fujay.langchain4jserver.domain.application.vo.AppConfigSnapshot;
import org.fujay.langchain4jserver.domain.application.vo.AppId;
import org.fujay.langchain4jserver.domain.knowledge.vo.KnowledgeBaseId;
import org.fujay.langchain4jserver.domain.model.vo.ModelId;

import java.time.LocalDateTime;

/**
 * 应用实体，聚合根的一部分
 */
@Getter
@Setter(AccessLevel.PRIVATE) // 状态变更通过方法
@AllArgsConstructor(access = AccessLevel.PRIVATE) // 通过聚合根或工厂创建
public class App {

    @NonNull
    private AppId id;

    @NonNull
    private String appName;

    private String appDesc;

    @NonNull
    private ModelId modelId;

    private boolean useKnowledgeBase;

    private KnowledgeBaseId knowledgeBaseId;

    private LocalDateTime createTime;
    private LocalDateTime updateTime;

    // 工厂方法
    public static App create(AppId id, String appName, String appDesc, ModelId modelId) {
        App app =
                new App(
                        id,
                        appName,
                        appDesc,
                        modelId,
                        false, // 默认不使用知识库
                        null, // 知识库ID为null
                        LocalDateTime.now(),
                        LocalDateTime.now());
        // 验证
        validateConfig(app.modelId, app.useKnowledgeBase, app.knowledgeBaseId);
        // 可发布 AppCreatedEvent
        return app;
    }

    // 加载方法 (供仓储使用)
    public static App load(
            AppId id,
            String appName,
            String appDesc,
            ModelId modelId,
            boolean useKnowledgeBase,
            KnowledgeBaseId knowledgeBaseId,
            LocalDateTime createTime,
            LocalDateTime updateTime) {
        validateConfig(modelId, useKnowledgeBase, knowledgeBaseId);
        return new App(
                id, appName, appDesc, modelId, useKnowledgeBase, knowledgeBaseId, createTime, updateTime);
    }

    // 静态验证逻辑
    private static void validateConfig(ModelId modelId, boolean useKb, KnowledgeBaseId kbId) {
        if (modelId == null) {
            throw new IllegalArgumentException("App must have a ModelId.");
        }
        if (useKb && kbId == null) {
            throw new IllegalArgumentException(
                    "KnowledgeBaseId cannot be null when useKnowledgeBase is true.");
        }
        if (!useKb && kbId != null) {
            throw new IllegalArgumentException(
                    "KnowledgeBaseId must be null when useKnowledgeBase is false.");
        }
    }

    // 更新应用基本信息
    public void updateBasicInfo(String newName, String newDesc) {
        this.appName = newName;
        this.appDesc = newDesc;
        this.updateTime = LocalDateTime.now();
    }

    // 更新应用配置
    public void updateConfiguration(ModelId newModelId, boolean useKb, KnowledgeBaseId kbId) {
        validateConfig(newModelId, useKb, kbId); // 验证新配置
        this.modelId = newModelId;
        this.useKnowledgeBase = useKb;
        this.knowledgeBaseId = kbId;
        this.updateTime = LocalDateTime.now();
    }

    // 获取当前配置快照
    public AppConfigSnapshot getCurrentConfigSnapshot() {
        return AppConfigSnapshot.builder()
                .modelId(this.modelId)
                .useKnowledgeBase(this.useKnowledgeBase)
                .knowledgeBaseId(this.knowledgeBaseId)
                .build();
    }
}
